<?php

/**
 *  Ham sinh ma captcha
 * 
 * */
if (!function_exists('create_captcha'))
{
    function create_captcha($img_path = 'images/captcha', $img_width = '120', $img_height = '40', $characters = '6', $font = 'monofont.ttf')
    {
        $expiration = 100; // thoi han ton tai anh captcha (don vi giay-s)
        $font       = realpath(dirname(__file__) . '/' . $font);
        $img_url    = $img_path;
        $img_path   = realpath(SITE_PATH . '/public/' . $img_path);

        if (!@is_dir($img_path))
        {
            return false;
        }

        if (!is_writable($img_path))
        {
            return false;
        }

        if (!extension_loaded('gd'))
        {
            return false;
        }

        if (!is_file($font))
        {
            return false;
        }

        // -----------------------------------
        // Remove old images
        // -----------------------------------

        list($usec, $sec) = explode(" ", microtime());
        $now = ((float)$usec + (float)$sec);

        $current_dir = @opendir($img_path);

        while ($filename = @readdir($current_dir))
        {
            if ($filename != "." and $filename != ".." and $filename != "index.html")
            {
                $name = str_replace(".jpg", "", $filename);

                if (($name + $expiration) < $now)
                {
                    @unlink($img_path . '/' . $filename);
                }
            }
        }

        @closedir($current_dir);


        //sinh ngau nhien code
        $possible = '123456789bcdfghjkmnpqrstvwxyz';
        $code = '';
        $i = 0;
        while ($i < $characters)
        {
            $code .= substr($possible, mt_rand(0, strlen($possible) - 1), 1);
            $i++;
        }

        $font_size = $img_height * 0.75;
        $image = @imagecreate($img_width, $img_height) or die('Cannot initialize new GD image stream');

        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);
        $text_color = imagecolorallocate($image, 20, 40, 100);
        $noise_color = imagecolorallocate($image, 100, 120, 180);

        /* generate random dots in background */
        for ($i = 0; $i < ($img_width * $img_height) / 3; $i++)
        {
            imagefilledellipse($image, mt_rand(0, $img_width), mt_rand(0, $img_height), 1, 1,
                $noise_color);
        }
        /* generate random lines in background */
        for ($i = 0; $i < ($img_width * $img_height) / 150; $i++)
        {
            imageline($image, mt_rand(0, $img_width), mt_rand(0, $img_height), mt_rand(0, $img_width),
                mt_rand(0, $img_height), $noise_color);
        }
        /* create textbox and add text */
        $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function');
        $x = ($img_width - $textbox[4]) / 2;
        $y = ($img_height - $textbox[5]) / 2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function');

        $img_name = $now . '.jpg';

        imagejpeg($image, $img_path . '/' . $img_name);

        $registry = Core_Registry::singleton();
        $img_url = $registry['baseUrl'] . '/public/' . $img_url . '/' . $img_name;
        $img = "<img src=\"$img_url\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
        imagedestroy($image);

        return array('code' => $code, 'time' => $now, 'image' => $img);
    }
}
